﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;

namespace University.Workshop.WinApp.Clients
{
    public partial class FormClientsView : Form
    {
        //-3,-22
        public static string value;
        Variables flag = Variables.Instance;
        ReportSaleVariables report = ReportSaleVariables.Instance;
        SaleVariables varible = SaleVariables.Instance;
        public FormClientsView()
        {
            InitializeComponent();
            disableAllTextBox("nit");
        }

        private void FormClientsView_Load(object sender, EventArgs e)
        {
            if (flag.Client == "BrowserClientSale")
            {
                tabControlClient.SelectedTab = tabPageSearch;
                buttonModifyClients.Visible = false;
                buttonViewClient.Visible = false;
                buttonDeleteClient.Visible = false;
                buttonAddClient.Visible = true;
            }
            if (report.IdClient == "BrowserClientReportSale")
            {
                tabControlClient.SelectedTab = tabPageSearch;
                buttonAddClient.Size = new Size(0, 0);
                buttonModifyClients.Size = new Size(0, 0);
                buttonViewClient.Size = new Size(0, 0);
                buttonDeleteClient.Size = new Size(0, 0);
                buttonAddClient.Size = new Size(0, 0);
            }
        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageRegisterClient;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageMainClient;
            CleanAllRegisterClientFills();
        }

        private void buttonAcept_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxNIT.Text) || string.IsNullOrEmpty(textBoxName.Text) || string.IsNullOrEmpty(textBoxPatLastName.Text) || string.IsNullOrEmpty(textBoxMatLastName.Text))
            {
                MessageBox.Show(this, "Faltan llenar campos obligatorios, revise e ingrese, por favor!", "Campos Obligarios incompletos", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                if (ClientController.ReviewIdIfNitRegister(textBoxNIT.Text))
                {
                    if(ClientController.RegisterClient(textBoxNIT.Text, textBoxName.Text, textBoxPatLastName.Text, textBoxMatLastName.Text, textBoxPhone.Text, textBoxCellphone.Text, textBoxEmail.Text, textBoxAdress.Text))
                    {
                        MessageBox.Show(this, "Cliente registrado satisfactoriamente!", "Almacenado", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                        CleanAllRegisterClientFills();
                        tabControlClient.SelectedTab = tabPageMainClient;
                    }
                    else
                        MessageBox.Show(this, "No se Puede registrar Cliente, revise e ingrese, por favor!", "Email Invalido  ", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
                else
                    MessageBox.Show(this, "El NIT-CI ingresado ya existe, verifique en la lista de clientes o re-ingrese el campo!", "Atributo existente", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
        }

        private void CleanAllRegisterClientFills()
        {
            textBoxNIT.Clear();
            textBoxName.Clear();
            textBoxPatLastName.Clear();
            textBoxMatLastName.Clear();
            textBoxPhone.Clear();
            textBoxCellphone.Clear();
            textBoxAdress.Clear();
            textBoxEmail.Clear();
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Validator_Only_Numbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            Validator.ValidatorOnlyNumbers(sender, e);
        }

        private void Validator_only_Characters_KeyPress(object sender, KeyPressEventArgs e)
        {
            Validator.ValidatorOnlyCharacters(sender, e);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageSearch;
        }

        private void radioButtonMatLastName_CheckedChanged(object sender, EventArgs e)
        {
            var o = (RadioButton)sender;
            foreach (var control in tabPageSearch.Controls)
            {
                if (control is TextBox && ((TextBox)control).Tag == o.Tag)
                {
                    ((TextBox)control).Enabled = o.Checked;
                    ((TextBox)control).Focus();
                }
            }
        }

        private void disableAllTextBox(string tag)
        {
            foreach (var control in tabPageSearch.Controls)
            {
                if (control is TextBox)
                {
                    bool enabled = (string)((TextBox)control).Tag == tag;
                    ((TextBox)control).Enabled = enabled;
                }
            }
        }

        private void buttonSearchCancel_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageMainClient;
            buttonSearchClean.PerformClick();
            report.IdClient = string.Empty;
        }

        private void buttonSearchClean_Click(object sender, EventArgs e)
        {
            ClearClientDate();
        }
        public void ClearClientDate()
        {
            textBoxSearchNit.Text = "";
            textBoxSearchName.Text = "";
            textBoxSearchMatLastName.Text = "";
            textBoxSearchPatLastName.Text = "";
            this.clientTableAdapter.FillByName(this.clientDataSet.Client, "-");
        }

        private void buttonGoSearch_Click(object sender, EventArgs e)
        {
            if (radioButtonNit.Checked)
            {
                try
                {
                    this.clientTableAdapter.FillByNit(this.clientDataSet.Client,"%" +textBoxSearchNit.Text + "%");
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
            else
            {
                if (radioButtonName.Checked)
                {
                    try
                    {
                        this.clientTableAdapter.FillByName(this.clientDataSet.Client, "%" + textBoxSearchName.Text + "%");
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    if (radioButtonPatLastName.Checked)
                    {
                        try
                        {
                            this.clientTableAdapter.FillBySurname1(this.clientDataSet.Client, "%" + textBoxSearchPatLastName.Text + "%");
                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                    else
                        if (radioButtonMatLastName.Checked)
                        {
                            try
                            {
                                this.clientTableAdapter.FillBySurname2(this.clientDataSet.Client, "%" + textBoxSearchMatLastName.Text + "%");
                            }
                            catch (System.Exception ex)
                            {
                                System.Windows.Forms.MessageBox.Show(ex.Message);
                            }
                        }
                }
            }
                
        }

        private void tabPageMainClient_Click(object sender, EventArgs e)
        {

        }

        private void buttonViewClient_Click(object sender, EventArgs e)
        {
            if (clientDataGridView.Rows.Count != 0)
            {
                textBoxViewNit.Text = clientDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                textBoxViewName.Text = clientDataGridView.SelectedRows[0].Cells[1].Value.ToString();
                textBoxViewSurname1.Text = clientDataGridView.SelectedRows[0].Cells[2].Value.ToString();
                textBoxViewSurname2.Text = clientDataGridView.SelectedRows[0].Cells[3].Value.ToString();
                textBoxViewTelephone.Text = clientDataGridView.SelectedRows[0].Cells[4].Value.ToString();
                textBoxViewCellPhone.Text = clientDataGridView.SelectedRows[0].Cells[5].Value.ToString();
                textBoxViewEmail.Text = clientDataGridView.SelectedRows[0].Cells[6].Value.ToString();
                textBoxViewAddress.Text = clientDataGridView.SelectedRows[0].Cells[7].Value.ToString();
                tabControlClient.SelectedTab = tabPageLookClient;
            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun cliente", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
            }
        }

        private void buttonDeleteClient_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = clientDataGridView.CurrentRow;
            if (clientDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    MessageBox.Show(this, "El cliente fue Eliminado", "Eliminar Cliente", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    bool controler = ClientController.DeleteClient(value);
                 }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }
          
           
        }

        private void buttonQuitView_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageSearch;

        }

        private void buttonModifyCancel_Click(object sender, EventArgs e)
        {
            tabControlClient.SelectedTab = tabPageMainClient;
        }

        private void buttonModClean_Click(object sender, EventArgs e)
        {
            ClearClientDate();
        }

        private void buttonModifyClients_Click(object sender, EventArgs e)
        {
            if (clientDataGridView.Rows.Count != 0)
            {
                tabControlClient.SelectedTab = tabPageModifyClients;
                textBoxModifyNit.Text=clientDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                textBoxModifyName.Text = clientDataGridView.SelectedRows[0].Cells[1].Value.ToString();
                textBoxModifyApPat.Text = clientDataGridView.SelectedRows[0].Cells[2].Value.ToString();
                textBoxModifyApMat.Text = clientDataGridView.SelectedRows[0].Cells[3].Value.ToString();
                textBoxModifyPhone.Text = clientDataGridView.SelectedRows[0].Cells[4].Value.ToString();
                textBoxModifyCel.Text = clientDataGridView.SelectedRows[0].Cells[5].Value.ToString();
                textBoxModifyEMail.Text = clientDataGridView.SelectedRows[0].Cells[6].Value.ToString();
                textBoxModifyAdress.Text = clientDataGridView.SelectedRows[0].Cells[7].Value.ToString();
            }
            else
            {
                    MessageBox.Show(this, "No se selecciono ningun cliente", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
            }
            
        }

        private void buttonModifyClient_Click(object sender, EventArgs e)
        {
            if (checkBoxModifyName.Checked || checkBoxModifyApPat.Checked || checkBoxModifyApMat.Checked || checkBoxModifyPhone.Checked || checkBoxModifyCel.Checked || checkBoxModifyEMail.Checked || checkBoxModifyAdress.Checked)
            {
                if (checkBoxModifyName.Checked)
                {
                    try
                    {

                        if (ClientController.ModifyNameClient(textBoxModifyName.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyApPat.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyPatNameClient(textBoxModifyApPat.Text, textBoxModifyNit.Text) == true)
                        {

                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyApMat.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyMatNameClient(textBoxModifyApMat.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyPhone.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyPhoneClient(textBoxModifyPhone.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyCel.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyCellClient(textBoxModifyCel.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyEMail.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyEmailClient(textBoxModifyEMail.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                if (checkBoxModifyAdress.Checked)
                {
                    try
                    {
                        if (ClientController.ModifyAddressClient(textBoxModifyAdress.Text, textBoxModifyNit.Text) == true)
                        {
                            this.clientTableAdapter.FillByNit(this.clientDataSet.Client, "%" + " " + "%");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                        else
                        {
                            MessageBox.Show("Error no se pudo modificar cliente intentelo de nuevo");
                            tabControlClient.SelectedTab = tabPageSearch;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }
                MessageBox.Show("Cliente Modificado satisfactoriamente");
            }
            else
                MessageBox.Show("tiene que seleccionar algun campo por favor");

        }

        private void buttonAddClient_Click(object sender, EventArgs e)
        {
            if (clientDataGridView.Rows.Count != 0)
            {
                varible.IdClient = clientDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                varible.FullNameClient = clientDataGridView.SelectedRows[0].Cells[1].Value.ToString() + " " + clientDataGridView.SelectedRows[0].Cells[2].Value.ToString() + " " + clientDataGridView.SelectedRows[0].Cells[3].Value.ToString();
                Close();
            }
        }

        private void FormClientsView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (report.IdClient =="BrowserClientReportSale")
            {
                report.IdClient = string.Empty;
            }
        }

        private void clientDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
            report.IdClient = clientDataGridView.SelectedRows[0].Cells[0].Value.ToString();
            Close();
            
        }

        
      }
}
       